<template>
	<view class="person-msg">
		<view class="head c-content" @click="changeFace">
			<image :src="form.face || '/static/missing-face.png'" mode=""></image>
			<view>点击修改头像</view>
		</view>
		<uni-forms :modelValue="form" ref="uForm" class="form mx-2">
			<uni-forms-item label="昵称" label-width="150" class="border-bottom">
				<input v-model="form.nickname" placeholder="请输入昵称" style="height: 36px;" class="fs-28" />
			</uni-forms-item>
			<uni-forms-item label="性别" label-width="150" class="border-bottom">
				<radio-group :activeBackgroundColor="lightColor" @change="radioChange">
					<radio value="1" :checked="form.gender == '1'" style="transform:scale(0.8)" class="fs-28">男</radio>
					<radio value="0" :checked="form.gender == '0'" style="transform:scale(0.8)">女</radio>
				</radio-group>
			</uni-forms-item>

			<uni-forms-item label="生日" label-width="150" right-icon="arrow-right" class="border-bottom">
				<picker mode="date" :value="form.birthday" :start="startDate" :end="endDate" @change="selectTime">
					<div style="width: 100%;" @click="showBirthday = true" class="fs-28">{{ form.birthday || '请选择出生日期' }}</div>
				</picker>
			</uni-forms-item>
			<!-- <uni-forms-item label="城市" label-width="150" placeholder="请选择城市" right-icon="arrow-right" class="border-bottom">
				<div style="width: 100%;" @click="clickRegion" class="fs-28">{{ form.___path || '请选择城市' }}</div>
			</uni-forms-item> -->

			<uni-forms-item label="手机号" label-width="150" class="border-bottom">
				<view v-if="form.mobile" class="fs-28">
					{{form.mobile}}
				</view>
				<view v-else class="fs-28">
					<view class="submit" @click="navigateTo(form.username)">绑定手机号码</view>
				</view>
			</uni-forms-item>

		</uni-forms>
		<div class="bottom">
			<view class="submit" @click="submit">保存</view>
			<view class="submit light" @click="quiteLoginOut">退出登录</view>
		</div>
		<m-city :provinceData="region" headTitle="区域选择" ref="cityPicker" @funcValue="getPickerParentValue"
			pickerSize="4"></m-city>
	</view>
</template>
<script>
	import {
		saveUserInfo,
		getUserInfo
	} from "@/api/members.js";
	import {
		upload
	} from "@/api/common.js";
	import storage from "@/utils/storage.js";
	import city from "@/components/m-city/m-city.vue";
	import api from '@/config/api.js'
	export default {
		components: {
			"m-city": city
		},
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				lightColor: this.$lightColor, //高亮颜色
				form: {
					nickname: storage.getUserInfo().nickname || "",
					birthday: storage.getUserInfo().birthday || currentDate,
					avatar: storage.getUserInfo().avatar || "/static/missing-face.png", //默认头像
					face: storage.getUserInfo().avatar || "/static/missing-face.png", //默认头像,
					gender: storage.getUserInfo().gender, //性别
					mobile: storage.getUserInfo().mobile,
					username: storage.getUserInfo().username,
				},
				photo: [{
						text: "立即拍照",
						color: this.$mainColor
					},
					{
						text: "从相册选择",
						color: this.$mainColor
					},
				],
				region: [
					//请求城市默认地址
					{
						id: "",
						localName: "请选择",
						children: [],
					},
				],
				showBirthday: false, //显示生日日期
			};
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		methods: {
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 10;
				} else if (type === 'end') {
					year = year + 10;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			radioChange: function(evt) {
				this.form.sex = evt.detail.value
			},
			/**
			 * 退出登录
			 */
			quiteLoginOut() {
				this.$options.filters.quiteLoginOut();
			},

			/**
			 * 选择地址回调
			 */
			getPickerParentValue(e) {
				this.form.region = [];
				this.form.regionId = [];
				let name = "";

				e.forEach((item, index) => {
					if (item.id) {
						this.form.region.push(item.localName);
						this.form.regionId.push(item.id);
						if (index == e.length - 1) {
							name += item.localName;
						} else {
							name += item.localName + ",";
						}
						this.form.___path = name;
					}
				});
			},

			/**
			 * 点击选择地址
			 */
			clickRegion() {
				this.$refs.cityPicker.show();
			},

			/**
			 * 提交保存
			 */
			submit() {
				delete this.form.___path;
				let params = JSON.parse(JSON.stringify(this.form));
				console.log('params-----', params)
				delete params.mobile
				saveUserInfo(params).then((res) => {
					if (res.code == 1) {
						this.getUserInfo()
					}
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				});
			},
			getUserInfo() {
				getUserInfo().then((res) => {
					console.log(res)
					const data = res.data
					if (data) {
						storage.setAccessToken(data.token);
						storage.setUserInfo(data);
						storage.setHasLogin(true);
						uni.navigateBack();
					}
				});
			},

			/**
			 * 修改头像
			 */
			changeFace(index) {
				uni.chooseImage({
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						uni.uploadFile({
							url: upload,
							filePath: tempFilePaths[0],
							name: "file",
							header: {
								'ba-user-token': storage.getAccessToken(),
							},
							success: (uploadFileRes) => {
								let data = JSON.parse(uploadFileRes.data);
								console.log(data)
								this.form.face = `${api.img}${data.data.file.url}`;
								this.form.avatar = `${api.img}${data.data.file.url}`;
							},
						});
					},
				});
			},

			/**
			 * 选择地址
			 */
			selectRegion(region) {
				this.$set(
					this.form,
					"address",
					`${region.province.label} ${region.city.label} ${region.area.label}`
				);
			},

			/**
			 * 选择时间
			 */
			selectTime(time) {
				console.log('time---', time)
				this.form.birthday = time.detail.value
			},

			navigateTo(username) {
				uni.navigateTo({
					url: '/pages/mine/set/securityCenter/bindMobile' + '?username=' + username,
				});
			},
		},

		/**
		 * 加载数据
		 */
		onLoad() {},
	};
</script>
<style>
	page {
		background: #fff;
	}
</style>
<style lang="scss" scoped>
	.submit {
		height: 90rpx;
		line-height: 90rpx;
		text-align: center;
		margin-top: 90rpx;

		width: 100%;
		margin: 0 auto;
		color: $main-color;
		border-radius: 100px;
	}

	.head {
		height: 260rpx;
		color: $font-color-light;
		font-size: $font-sm;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		line-height: 2em;

		image {
			width: 144rpx;
			height: 144rpx;
			border-radius: 50%;
		}
	}

	::v-deep .uni-forms {
		background-color: #ffffff;
		padding: 0;
		margin-top: 30rpx;

		.uni-forms-item {
			padding: 0 20rpx;
			line-height: 72rpx;
		}
	}

	.form {
		background-color: #ffffff;
	}

	.bottom {
		position: fixed;
		bottom: 40px;
		display: flex;
		align-items: center;
		width: 100%;

		>.submit {
			background: $light-color;
			color: #fff;
			width: 40%;
		}

	}

	.light {
		background: rgba($color: $light-color, $alpha: 0.2) !important;
		color: $light-color !important;
	}
</style>